<template>
    <el-dialog
      title="新增物流服务商"
      :visible.sync="dialogVisible"
      width="1050px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      custom-class="custom-class-line"
      @close="closeDialog">
    <BasicAdd ref="basic"></BasicAdd>
    <div class="text-center">
        <el-button type="primary" style="width:100px" @click="confirmAdd">确认新增</el-button>
        <el-button style="width:100px" @click="closeDialog">取消</el-button>
    </div>
    </el-dialog>
</template>

<script>
import BasicAdd from './basic/basicAdd.vue'
import { randomString,arrSubtraction } from "@/utils/zito"
import { addBossAddDept } from '@/api/monitor/server'
export default {
  name: 'add',
  components: {
    BasicAdd,
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'basic',
      form: {
        addressList: [
          {
            uuid: randomString(32)
          }
        ]
      },
      refList: [
        'basic',
      ],
    };
  },
  methods: {
    showDialog() {
      this.dialogVisible = true
    },
    closeDialog(e) {
      this.dialogVisible = false
      e === true && this.$emit('close')
    },
    async confirmAdd() {
      const valid = await this.$refs[this.activeName].handleValidate()
      if (valid) {
        let promiseArray = [];
        const remainRefList = arrSubtraction(this.refList, [this.activeName])
        for(let i = 0;i<remainRefList.length;i++){
          promiseArray.push(new Promise(async(resolve) => {
            const result = await this.$refs[remainRefList[i]].handleValidate()
            if (result) resolve(true)
            else resolve(false)
          }))
        }
        const resultArray = await Promise.all(promiseArray)
        const resultAll = resultArray.every(v => v)
        if (resultAll) {
          addBossAddDept(this.$refs.basic.form).then(res => {
            this.$message.success('新增成功')
            this.closeDialog(true)
          })
        } else {
          this.$message.error('请完善其他信息')
        }
      }
    }
  }
};
</script>
